package com.qd.qx.ui.module2;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qd.common.base.BaseFragment;
import com.qd.common.commonutils.ToastUitl;
import com.qd.qx.R;
import com.qd.qx.ui.module1.SearchActivity;
import com.qd.qx.utils.UIShowUtils;
import com.qd.qx.widget.BaseFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Code is far away from bug with the animal protecting
 * Author：ys on 2018/7/13
 * Function：通讯录模块
 */
public class AddressBookFragment extends BaseFragment {


    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivMore)
    ImageView ivMore;
    @BindView(R.id.rlSearch)
    RelativeLayout rlSearch;
    BaseFragmentPagerAdapter adapter;
    private List<String> tabList = new ArrayList<>();//tablayout 展示的集合

    private TXLChildFragment txlFragment, qzFragment, cylxrFragment;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_addressbook_layout;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        ivBack.setVisibility(View.GONE);
        tvTitle.setText("通讯录");
        ivMore.setVisibility(View.VISIBLE);
        tabList.add("通讯录");
        tabList.add("群组");
        tabList.add("常用联系人");
        viewPager.setOnPageChangeListener(new MyPagerChangeListener());
//
//把Fragment添加到List集合里面
        if (null == txlFragment) {
            txlFragment = new TXLChildFragment();
        }
        Bundle bundle1 = new Bundle();
        bundle1.putInt("type", 1);
        txlFragment.setArguments(bundle1);


        if (null == qzFragment) {
            qzFragment = new TXLChildFragment();
        }
        qzFragment = new TXLChildFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putInt("type", 2);
        qzFragment.setArguments(bundle2);

        if (null == cylxrFragment) {
            cylxrFragment = new TXLChildFragment();
        }
        cylxrFragment = new TXLChildFragment();
        Bundle bundle3 = new Bundle();
        bundle3.putInt("type", 3);
        cylxrFragment.setArguments(bundle3);

        List list = new ArrayList<>();
        list.add(txlFragment);
        list.add(qzFragment);
        list.add(cylxrFragment);
        adapter = new BaseFragmentPagerAdapter(getActivity().getSupportFragmentManager(), list, tabList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);  //初始化显示第一个页面
        // tabLayout.addTab(tabLayout.newTab().setText(tabList.get(0)));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                UIShowUtils.setIndicator(tabLayout, 20, 20);
            }
        });
    }


    /**
     * 设置一个ViewPager的侦听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    viewPager.setCurrentItem(0);
                    break;
                case 1:
                    viewPager.setCurrentItem(1);
                    break;
                case 2:
                    viewPager.setCurrentItem(2);
                    break;
            }
        }
    }

    @OnClick({R.id.rlSearch, R.id.ivMore})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rlSearch:
                SearchActivity.startAction(getActivity());
                break;
            case R.id.ivMore:
                TongXunLuSelectActivity.startAction(getActivity(), "0", "通讯录");
                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != txlFragment) {
            txlFragment.refresh(0);
        }

        if (null != qzFragment) {
            qzFragment.refresh(1);
        }
        if (null != cylxrFragment) {
            cylxrFragment.refresh(2);
        }

    }
}
